Completed
Push — master ( 0a8aee...45be8d )
by Andres
30s
created

angular.controller(ꞌmain-loopꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 6
rs 9.4285
nop 1
1
'use strict';
2
3
angular
4
  .module('game')
5
  .controller('main-loop', ['$scope',
6
    '$interval',
7
    '$timeout',
8
    'savegame',
9
    'state',
10
    'data',
11
    'util',
12
    function($scope, $interval, $timeout, savegame, state, data, util) {
13
      $scope.state = state;
14
15
      let self = this;
16
      let playerCopy = null;
17
18
      self.update = function() {
19
        // do the update in a copy
20
        playerCopy = angular.copy(state.player);
21
22
        state.update(playerCopy);
23
24
        // and update all at once
25
        state.player = playerCopy;
26
      };
27
28
      self.updateLoop = function() {
29
        self.update();
30
        $timeout(self.updateLoop, 1000);
31
      };
32
33
      self.processOffline = function() {
34
        let remaining = state.offlineCyclesTotal-state.offlineCyclesCurrent;
35
        let cycles = Math.min(32, remaining);
36
37
        for(let i = 0; i < cycles; i++){
38
          self.update();
39
          state.offlineCyclesCurrent++;
40
          remaining--;
41
        }
42
43
        if(remaining > 0 && !state.cancelOffline){
44
          $timeout(self.processOffline);
45
        }else{
46
          // we are done processing, turn off the screens
47
          state.processingOffline = false;
48
          state.loading = false;
49
          // trigger the game loop
50
          $timeout(self.updateLoop, 1000);
51
          $interval(savegame.save, 10000);
52
        }
53
      };
54
55
      self.startup = function() {
56
        savegame.load();
57
        let elapsed = Math.floor(Date.now()/1000)-state.player.last_login;
58
        // lets limit the offline elapsed time
59
        elapsed = Math.min(util.calculateValue(data.global_upgrades.offline_time.power.base,
60
            data.global_upgrades.offline_time.power,
61
            state.player.global_upgrades.offline_time), elapsed);
62
        state.offlineCyclesTotal = elapsed;
63
        state.offlineCyclesCurrent = 0;
64
        if(elapsed > 32){
65
          state.loading = false;
66
          state.processingOffline = true;
67
        }
68
        $timeout(self.processOffline);
69
      };
70
      
71
      $timeout(self.startup);
72
    }
73
  ]);
74